require 'iconv'
require 'open-uri'
require 'net/http'

class ListController < ApplicationController
  def index
    @action ='index'
    @categories = ItemCategory.lists(16,nil)
    @cid=params[:cid]
    @title = '女装'
    @category_name = ItemCategory.find_by_t_cid(@cid)
    @title = @category_name.t_cat_name unless @category_name.nil?
    page=params[:page]
    page = 1 if !params[:page].nil? && params[:page].length==0
    
    #品牌
    @brands = PropertyValue.property_values({:cid=>@cid,:pid=>20000,:order=>'sort_order'})
    #价格
    @price = PropertyValue.property_values({:cid=>@cid,:pid=>21541,:order=>'sort_order'})
    #样式
    @styles = PropertyValue.property_values({:cid=>@cid,:pid=>20661,:order=>'sort_order'})
    #店铺
    #@top_shops = TaobaokeShop.top(14)
    #显示对象
    options={}
    #按类别和属性查找
    if !params[:props].nil? && !params[:props].empty?
      options[:cid]=@cid
      options[:prop]=params[:props]
    #列出卖家所有商品 
    elsif @cid.nil? && !params[:nick].nil?
        options[:nick]=params[:nick]
    else
      options[:cid]=@cid
    end
    puts params[:keyword]
    if !params[:keyword].nil?
      option={}
      option[:price_from]=params[:price_from]
      option[:price_to]=params[:price_to]
      @items = Item.valid_items.search(options).keyword(params[:keyword],nil,option).paginate :page=>page,:per_page=>33
      @title += '_'+params[:keyword]
    else
      @items = Item.valid_items.search(options).paginate :page=>page,:per_page=>33
    end
    
    params[:props].reverse_each do |p|
      @title +='_'+PropertyValue.find_by_vid(p.split(':')[1]).name
    end unless params[:props].nil?
    
    
  end
  
  def all
    redirect_to :controller => "list", :action => "index"
  end

  def hanban
    keyword = '韩'
    keyword += '%'+ params[:keyword] unless params[:keyword].nil?
    keywords(keyword,params[:categories],params[:page],'hanban','')
    @title = '韩版女装'
    @title +='_'+params[:keyword] unless params[:keyword].nil?
    render 'index'
  end
  
  def nvqun
    @title = '女裙'
    keywords(params[:keyword],params[:categories],params[:page],'nvqun',@title)
    render 'index'
  end

  def nvku
    @title = '女裤'
    keywords(params[:keyword],params[:categories],params[:page],'nvku',@title)
    render 'index'
  end
  
  def search_by_keywords
    keyword = params[:word]
    keyword += '_'+params[:keyword] unless params[:keyword].nil?
    keywords(keyword,params[:categories],params[:page],'search_by_keywords')
    @title=keyword
    render 'index'
  end
  
  def keywords(keyword,categories,page,action,title = '')
    @action=action
    @categories = ItemCategory.lists(16,nil)
    #@top_shops = TaobaokeShop.top(14)
    option={}
    option[:price_from]=params[:price_from]
    option[:price_to]=params[:price_to]
    
    page = 1 if page.blank?
    @items = Item.valid_items.keyword(keyword,categories,option).paginate :page=>page,:per_page=>33
    @title=title+'_'+keyword unless keyword.nil?
  end

	private
	def url_encode(str)
		return str.gsub!(/[^\w$&\-+.,\/:;=?@]/) { |x| x = format("%%%x", x[0])}  
	end
	
	def to_url_gbk(str)
		Iconv.iconv("GBK//IGNORE","utf-8//IGNORE",str).to_s
	end

  
end
